package checkers3d.logic;

import checkers3d.storage.DataManagerGame;
import checkers3d.storage.ScenarioManager;
import java.awt.Point;

/**
 * Allows for two players to login and start playing a scenario.
 *
 * @author Ruben Acuna
 */
public class MenuScenarioLoginMP extends WindowLoginMP
{
    public MenuScenarioLoginMP(Point size, String scenarioName)
    {
        super(size);

        setLoginObserver(new ObserverLogin(scenarioName));
    }

    /**
     * A observer for a button that creates and displays a new game object based
     * on the selected scenario.
     */
    private class ObserverLogin implements IInputObserverLogin
    {
        /**
         * The name of the scenario that was selected.
         */
        private String scenarioName;

        /**
         * The home player.
         */
        private Player home;

        /**
         * The visitor player.
         */
        private Player visitor;

        /**
         * Class constructor that keeps a reference to the parent object.
         *
         * @param parent Reference to the parent MenuScenarioSelect object.
         */
        public ObserverLogin(String scenarioName)
        {
            this.home = null;
            this.visitor = null;
            this.scenarioName = scenarioName;
        }

        /**
         * Creates and displays a new game object based on the selected scenario.
         *
         * @param position Location of click within button.
         */
        public void onClick(Point position)
        {
            if(home != null && visitor != null)
            {
                GameMaster.getInstance().removeTopGUIContainer();
                
                try
                {
                    ScenarioManager.loadScenario(ScenarioManager.LOCATION_SCENARIOS + scenarioName + ".txt", home, visitor);
                }
                catch(Exception err)
                {
                    //do nothing.
                }
            }
        }

         /**
         * Key press observer that does nothing.
         *
         * @param key Key that was pressed.
         */
        public void onKeyPress(char key)
        {
            //do nothing.
        }

        /**
         * Sets the last validated home.
         *
         * @param player The home player.
         */
        public void setHome(Player player)
        {
            home = player;
        }

        /**
         * Sets the last validated visitor.
         *
         * @param player The visiting player.
         */
        public void setVisitor(Player player)
        {
            visitor = player;
        }
    }
}